---
id: pluginmqttserver
title: MqttServer
---

## 一、设备扩展属性

![](/img/docs/pluginmqttserver-2.png)

| 属性           |  说明                    | 默认值/备注|
| ---------------| --------------------------| ---|
| IP         | ServerIP,为空时指任意IP              | |
| 端口         | 连接端口              | 1883|
| 允许连接的ID前缀         | 过滤ID，例如Client端ID必须是ThingsGatewayId123456              | ThingsGatewayId |
| 允许Rpc写入         | 是否允许写入变量              | True |
| 线程循环间隔         | 上传线程的循环间隔,一般不需要更改(ms)              | 1000 |
| 列表分割大小         | 发布的实体列表按大小分割              | 1000 |
| 设备Topic         | 设备信息订阅Topic              | ThingsGateway/Device |
| 变量Topic         | 变量信息订阅Topic              | ThingsGateway/Variable |
| Rpc返回Topic         | 客户端写入变量的订阅Topic，返回写入结果信息              | ThingsGateway/RpcSub |
| Rpc写入Topic         | 客户端写入变量的发布Topic              | ThingsGateway/RpcWrite |
:::tip

 登录网关的MqttServer还需要配置第三方授权User，并且在客户端填写对应的UserName与Password

![](/img/docs/pluginmqttserver-1.png)
:::

## 二、变量配置

<img src={require('../static/img/docs/pluginmqttserver-3.png').default} width="400" />


### 启用

启用变量后才能进行上传

### 允许写入

单独配置变量是否允许写入


## 三、实体脚本

### 说明
脚本功能由cs-script实现

```
 //提供这个例子

 //定义返回List
  List<dynamic> newModelList=new List<dynamic>(); 
  //input为固定传入值，在变量脚本中为变量实体类List，在设备脚本中为设备实体类List，查看上面的json说明
  foreach (var item in input)
  {
    //添加自定义值
    newModelList.Add(new
  {
      customName=item.Name,//变量名称
      customValue=item.Value,//变量值
      customCollectTime=item.CollectTime.ToString("yyyy-MM-dd HH:mm:ss fffffff"), //采集时间，这里格式化为自定义时间格式
  });

  }
  return newModelList;

```

<img src={require('../static/img/docs/script2.png').default} width="400" />

可以看到mqtt上传内容已经改为以上脚本返回值

### DTO定义

```
#region copyright
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
#endregion


using ThingsGateway.Core;
namespace ThingsGateway.Web.Foundation;
/// <summary>
/// 上传DTO
/// </summary>
public class VariableData
{
    /// <inheritdoc cref="PrimaryIdEntity.Id"/>
    public long Id { get; set; }
    /// <inheritdoc cref="MemoryVariable.Name"/>
    public string Name { get; set; }
    /// <inheritdoc cref="MemoryVariable.Description"/>
    public string Description { get; set; }
    /// <inheritdoc cref="DeviceVariableRunTime.DeviceName"/>
    public string DeviceName { get; set; }
    /// <inheritdoc cref="DeviceVariableRunTime.RawValue"/>
    public object RawValue { get; set; }
    /// <inheritdoc cref="DeviceVariableRunTime.Value"/>
    public object Value { get; set; }
    /// <inheritdoc cref="DeviceVariableRunTime.ChangeTime"/>
    public DateTime ChangeTime { get; set; }
    /// <inheritdoc cref="DeviceVariableRunTime.CollectTime"/>
    public DateTime CollectTime { get; set; }
    /// <inheritdoc cref="DeviceVariableRunTime.IsOnline"/>
    public bool IsOnline { get; set; }
    /// <inheritdoc cref="MemoryVariable.ReadExpressions"/>
    public string ReadExpressions { get; set; }
    /// <inheritdoc cref="DeviceVariable.WriteExpressions"/>
    public string WriteExpressions { get; set; }
    /// <inheritdoc cref="DeviceVariable.IntervalTime"/>
    public int IntervalTime { get; set; }
    /// <inheritdoc cref="DeviceVariable.OtherMethod"/>
    public string OtherMethod { get; set; }
    /// <inheritdoc cref="DeviceVariable.VariableAddress"/>
    public string VariableAddress { get; set; }

    /// <inheritdoc cref="MemoryVariable.ProtectTypeEnum"/>
    public int ProtectTypeEnum { get; set; }
    /// <inheritdoc cref="MemoryVariable.DataTypeEnum"/>
    public int DataTypeEnum { get; set; }
}

```
```
#region copyright
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
#endregion


using ThingsGateway.Core;
namespace ThingsGateway.Web.Foundation;
/// <summary>
/// 设备上传DTO
/// </summary>
public class DeviceData
{
    /// <inheritdoc cref="PrimaryIdEntity.Id"/>
    public long Id { get; set; }
    /// <inheritdoc cref="CollectDeviceRunTime.PluginName"/>
    public string PluginName { get; set; }
    /// <inheritdoc cref="CollectDeviceRunTime.DeviceVariableCount"/>
    public int DeviceVariablesCount { get; set; }
    /// <inheritdoc cref="CollectDeviceRunTime.ActiveTime"/>
    public DateTime ActiveTime { get; set; }
    /// <inheritdoc cref="CollectDeviceRunTime.DeviceStatus"/>
    public int DeviceStatus { get; set; }
    /// <inheritdoc cref="CollectDeviceRunTime.LastErrorMessage"/>
    public string LastErrorMessage { get; set; }
    /// <inheritdoc cref="UploadDevice.Name"/>
    public string Name { get; set; }
    /// <inheritdoc cref="UploadDevice.Description"/>
    public string Description { get; set; }
    /// <inheritdoc cref="CollectDeviceRunTime.KeepRun"/>
    public bool KeepRun { get; set; }

}


```

## 四、RPC写入格式

```

/// <summary>
/// MqttRpc传入
/// </summary>
public class MqttRpcNameVaueWithId
{
    /// <summary>
    /// 标识
    /// </summary>
    public string RpcId { get; set; }
    /// <summary>
    /// "WriteInfos":{"test":"1"}
    /// </summary>
    public Dictionary<string, string> WriteInfos { get; set; } = new();
}
/// <summary>
/// MqttRpc输出
/// </summary>
public class MqttRpcResult
{
    /// <summary>
    /// 标识
    /// </summary>
    public string RpcId { get; set; }
    /// <summary>
    /// 消息
    /// </summary>
    public Dictionary<string, OperResult> Message { get; set; } = new();
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }
}


```